package com.quanyan.club.service.impl;

import com.quanyan.club.entity.db.*;
import com.quanyan.club.entity.vo.resp.MedalRule;
import com.quanyan.club.entity.vo.resp.RespClubMedal;
import com.quanyan.club.entity.vo.resp.RespClubMedalRule;
import com.quanyan.club.mapper.ClubMedalExtendMapper;
import com.quanyan.club.mapper.TbClubMedalRelationMapper;
import com.quanyan.club.mapper.TbMedalMapper;
import com.quanyan.club.service.BaseService;
import com.quanyan.club.service.ClubMedalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by zhang on 16/6/3.
 */
@Service("clubMedalService")
public class ClubMedalServiceImpl extends BaseServiceImpl implements ClubMedalService {

    private final static Logger logger = LoggerFactory.getLogger(ClubMedalServiceImpl.class);

    @Autowired
    private TbMedalMapper medalMapper;

    @Autowired
    private ClubMedalExtendMapper clubMedalExtendMapper;

    @Autowired
    private TbClubMedalRelationMapper clubMedalRelationMapper;
    ReentrantLock lock = new ReentrantLock(true);
    /**
     * 获取俱乐部勋章列表
     * @param clubId
     * @param type
     * @return
     */
    @Override
    public List<RespClubMedal> listClubMedal(int clubId, int type) {
        List<RespClubMedal> list;
        //type说明：1为已获得的勋章，2为未获得的勋章
        Map map = new HashMap();
        map.put("clubId",clubId);
        map.put("type",type);
        List<TbClubMedalRelation> clubMedalRelationList;
        try {
            lock.lock();
            clubMedalRelationList = this.getClubMedalRelationList(clubId);
        } finally {
            lock.unlock();
        }
        if (clubMedalRelationList.isEmpty()) {
            TbClub club = super.getClub(clubId);
            if (club != null) {
                int n = 0;
                try{
                    n = initClubMedal(clubId,club.getClubType());
                } catch (Exception e) {
                    logger.error("勋章插入失败失败：clubId：【{}】,异常信息为",clubId,e);
                }
                if (n > 0) {
                    list = clubMedalExtendMapper.listClubMedal(map);
                } else {
                    list = new ArrayList<>();
                    logger.error("您初始化的俱乐部勋章失败：clubId：【{}】",clubId);
                }
            } else {
                list = new ArrayList<>();
                logger.error("您初始化的俱乐部不存在：clubId：【{}】",clubId);
            }

        } else {
            list = clubMedalExtendMapper.listClubMedal(map);

        }

        return list;
    }

    /**
     * 获取勋章规则详情
     * @param medalId
     * @return
     */
    @Override
    public RespClubMedalRule getMedalRule(int medalId) {
        RespClubMedalRule clubMedalRule = new RespClubMedalRule();
        List<MedalRule> ruleList = clubMedalExtendMapper.listMedalRule(medalId);
        TbMedal medal = medalMapper.selectByPrimaryKey(medalId);
        if(null != medal){
            clubMedalRule.setRuleList(ruleList);
            clubMedalRule.setMedalId(medal.getId());
            clubMedalRule.setIcon(medal.getIcon());
            clubMedalRule.setName(medal.getName());
        }
        return clubMedalRule;
    }

    /**
     * 获取俱乐部勋章关联信息
     * @param clubId
     * @return
     */
    @Override
    public List<TbClubMedalRelation> getClubMedalRelationList(Integer clubId) {
        TbClubMedalRelationExample example = new TbClubMedalRelationExample();
        example.createCriteria().andClubIdEqualTo(clubId);
        List<TbClubMedalRelation> clubMedalRelationList = this.clubMedalRelationMapper.selectByExample(example);
        return clubMedalRelationList;
    }

    /**
     * 获取勋章列表
     * @param medalType
     * @return
     */
    @Override
    public List<TbMedal> getMedalList(Integer medalType, List<Integer> categoryIdList) {
        TbMedalExample example = new TbMedalExample();
        //勋章类型：1为你俱乐部，2为个人
        TbMedalExample.Criteria criteria = example.createCriteria();
        criteria.andMedalTypeEqualTo(medalType.byteValue());
        criteria.andIsDeleteEqualTo(false);
        if (!categoryIdList.isEmpty()) {
            criteria.andCategoryIdIn(categoryIdList);
        }
        List<TbMedal> medalList = this.medalMapper.selectByExample(example);
        return medalList;
    }

    /**
     * 批量插入俱乐部勋章关联信息
     * @param clubMedalRelationList
     * @return
     */
    @Override
    public int batchInsertClubMedalRelation(List<TbClubMedalRelation> clubMedalRelationList) {
        int n = 0;
        for (TbClubMedalRelation clubMedalRelation : clubMedalRelationList) {
            clubMedalRelationMapper.insertSelective(clubMedalRelation);
            n++;
        }
        return n;
    }

    /**
     * 初始化俱乐部勋章
     * @param clubId
     * @param categoryId
     * @return
     */
    @Override
    public int initClubMedal(Integer clubId,Integer categoryId){
        logger.info("开始初始化俱乐部勋章 clubId：【{}】 categoryId：【{}】",clubId,categoryId);
        /** -----------------------------------------初始化俱乐部勋章开始------------------------------------- **/
        List<TbClubMedalRelation> clubMedalRelations = new ArrayList<>();
        List<TbClubMedalRelation> clubMedalRelationList = this.getClubMedalRelationList(clubId);
        int n = 0;
        if (clubMedalRelationList.isEmpty()) {
            //勋章类型：1为俱乐部，2为个人
            List<Integer> categoryIdList = new ArrayList<>();
            categoryIdList.add(0);
            categoryIdList.add(categoryId);

            List<TbMedal> medalList = this.getMedalList(1, categoryIdList);

            TbClubMedalRelation clubMedalRelation;
            for (TbMedal tbMedal : medalList) {
                clubMedalRelation = new TbClubMedalRelation();
                clubMedalRelation.setClubId(clubId);
                clubMedalRelation.setMedalGetDate(new Date());
                clubMedalRelation.setGoalValue(tbMedal.getMaxStandardValue());
                clubMedalRelation.setMedalId(tbMedal.getId());
                clubMedalRelation.setOwnValue(0);
                clubMedalRelation.setMedalStartLevel((byte)0);
                clubMedalRelations.add(clubMedalRelation);
            }
            n = this.batchInsertClubMedalRelation(clubMedalRelations);
        }
        logger.info("完成初始化俱乐部勋章 clubId：【{}】 categoryId：【{}】",clubId,categoryId);
        /** ------------------------------------------初始化俱乐部勋章结束------------------------------------ **/
        return n;
    }


}
